extends State
class_name Playerjump
@onready var player_sprite: AnimatedSprite2D = $"../../PlayerSprite"
@onready var playerjump: Node = $"."
var gravity = Autoload.gravity
const jump_speed:float =300
@onready var ray_cast_2d_2: RayCast2D = %RayCast2D2
@onready var ray_cast_2d: RayCast2D = %RayCast2D

@export var jump_acceleration:float = 1000.0
@export var air_friction:float = 500.0
@export var max_jumping_level_speed:float = 180.0
@export var xVelocity:float = 200.0
@export var yVelocity:float = 420.0

var jumping:bool = 0
var jumpflag:bool = 0
var direction = Input.get_axis("玩家向左走","玩家向右走")
var jump_time:float = 0.05
var longjump_flag:bool = false

@onready var label: Label = $Label

func enter(): 
	jump_time = 0.05
	print("进入跳跃状态")
	if character.is_on_floor():
		Autoload.wolf_time = -100
		character.velocity.y = -jump_speed
		jumpflag = true

	elif Autoload.wolf_time > 0:
		character.velocity.y = -jump_speed
		jumpflag = true
	else:
		jumping = false
	jumping = true
	
func update(delta:float):
	Autoload.lock_time -= delta
	jump_time -= delta
	if character.is_on_floor() and jump_time <= 0:
		jumping = false
		jumpflag = false
		Autoload.wolf_time = Autoload.wolf_time_stander
		if Input.is_action_pressed("玩家向左走") or Input.is_action_pressed("玩家向右走"):
			transition_requested.emit("Playerwalk")
		else:
			transition_requested.emit("Playeridle")
		if Autoload.stage_show:
			transition_requested.emit("Playeridle")
	#if InputBuffer.has_buffered_input("冲刺") and Autoload.dashflag == true:
		#InputBuffer.consume_buffered_input("冲刺")
		#transition_requested.emit("Playerdash")
		
	if character.is_on_wall() and !character.is_on_floor() and jump_time <= 0:
		if Input.is_action_pressed("玩家向左走") or Input.is_action_pressed("玩家向右走"):
			transition_requested.emit("Playerwallslide")
	
		
		
	
func physics_update(delta:float):
	if character.velocity.y < 0:
		player_sprite.play("jump1")
		if Input.is_action_pressed("玩家跳跃"):
			longjump_flag = true
		else:
			longjump_flag = false
	else:
		player_sprite.play("jump2")
		longjump_flag = false
	if !character.is_on_floor():
		if longjump_flag:
			character.velocity.y +=  gravity * delta * 0.75
		else:
			character.velocity.y += gravity * delta
		if ray_cast_2d.is_colliding() or ray_cast_2d_2.is_colliding():
			if Input.is_action_just_pressed("玩家跳跃"):
				jumpflag = true
				var wall_direction = character.get_wall_normal().x
				Autoload.lock_time = 0.16
				if(wall_direction > 0):
					character.velocity.x = xVelocity
				else:
					character.velocity.x = -xVelocity
				character.velocity.y = -yVelocity
		Autoload.wolf_time -= delta
			
	#空中水平移动
	direction = Input.get_axis("玩家向左走","玩家向右走")
	if Autoload.lock_time < 0:
		if (direction > 0):
			player_sprite.flip_h = 0
		if (direction < 0):
			player_sprite.flip_h = 1

		if direction:
			character.velocity.x += direction * jump_acceleration * delta
			character.velocity.x = clamp(character.velocity.x,-max_jumping_level_speed,max_jumping_level_speed)
		else:
			if character.velocity.x > 0:
				character.velocity.x = max(0,character.velocity.x - air_friction * delta)
			elif character.velocity.x < 0:
				character.velocity.x = max(0,character.velocity.x + air_friction * delta)
	character.move_and_slide()





func _on_x速度变大_button_down() -> void:
	xVelocity += 10.0
	label.text = ("X:"+str(xVelocity)+"\n"+"Y:"+str(yVelocity))
	pass # Replace with function body.


func _on_x速度变小_button_down() -> void:
	xVelocity -= 10.0
	label.text = ("X:"+str(xVelocity)+"\n"+"Y:"+str(yVelocity))
	pass # Replace with function body.


func _on_y速度变大_button_down() -> void:
	yVelocity  += 10.0
	label.text = ("X:"+str(xVelocity)+"\n"+"Y:"+str(yVelocity))
	pass # Replace with function body.


func _on_y速度变小_button_down() -> void:
	yVelocity -= 10.0
	label.text = ("X:"+str(xVelocity)+"\n"+"Y:"+str(yVelocity))
	pass # Replace with function body.
